<HTML>
<!--
	THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
								J. Brook Monroe, mrprogguy@techie.com
    Copyright (c)1998 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

	Use at your own risk. No warranty is given or implied of the suitability 
	of this applet for any specific application. Neither Erica Sadun,
	J. Brook Monroe nor Charles River Media will be held responsible for any
	unwanted effects due to the use of this applet or any derivative. 
-->	
<HEAD>
	<TITLE>Getting All Keyed Up II</TITLE>
<SCRIPT LANGUAGE="JavaScript1.2"><!--
var lastKey;
function getKeyDown(evt)
{
	lastKey = evt.which;
	// Convert character code to string representation
	document.forms[0].key.value = '"'+String.fromCharCode(evt.which)+'"';
	var modString = "";
	mods = parseInt(evt.modifiers);
	if (mods & Event.ALT_MASK)		// 1
		modString += "ALT ";
	if (mods & Event.CONTROL_MASK)	// 2
		modString += "CTRL ";
	if (mods & Event.SHIFT_MASK)	// 4
		modString += "SHIFT ";
	if (mods & Event.META_MASK)		// 8???
		modString += "META";		
	document.forms[0].mods.value = modString;
	return false; // Stop KeyPress event!
}

function getKeyUp(evt)
{
	if(lastKey == evt.which) {
		document.forms[0].mods.value="";
		document.forms[0].key.value="";
	}
}

function Setup()
{
	window.captureEvents(Event.KEYDOWN | Event.KEYUP);
	window.onKeyDown=getKeyDown;
	window.onKeyUp=getKeyUp;
	window.focus();
}	

function TidyUp()
{
	window.releaseEvents(Event.KEYDOWN | Event.KEYUP);
}	
//-->
</SCRIPT>
</HEAD>
<BODY onUnload="TidyUp()">
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50 ALIGN = LEFT>Getting All Keyed Up II</H1></FONT>
<BLOCKQUOTE><FONT COLOR="770000">
You can now see keys being pressed and released!
</FONT>
<FORM>Key pressed: <INPUT TYPE="text" SIZE=4 NAME="key"> with keyboard modifiers <INPUT TYPE="text" SIZE=18 name="mods"><BR>
</FORM><p>
<SCRIPT LANGUAGE="JavaScript1.2">Setup()</SCRIPT>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
By capturing <FONT FACE="Arial,Helvetica" COLOR="#007777">onKeyDown</FONT> and <FONT FACE="Arial,Helvetica" COLOR="#007777">onKeyUp</FONT>
events, you can cause script responses that occur only as long as a key is held down.  In this script, we just clear the form whenever we detect that a key has been released.<P>
One interesting feature about this event set is that if your <FONT FACE="Arial,Helvetica" COLOR="#007777">onKeyDown</FONT> handler
returns <i>false</i>, JavaScript won't generate <KBD><B>KEYPRESS</B></KBD> events.  (If you're already getting events via <KBD><B>KEYDOWN</B></KBD> handlers, you probably don't want to see <KBD><B>KEYPRESS</B></KBD> events anyway!)
</FONT>
<BR><BR><h5>Copyright ©1998 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>